package com.qifan.alb2b.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.JdbcUtil;
import com.qifan.alb2b.action.back.param.RoomQueryParam;
import com.qifan.alb2b.domain.RoomInfoDomain;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.DateUtil;
import com.qifan.alb2b.util.Page;

/**
 * 房间CURD的Dao
 * @author hc24
 *
 */
@Repository
public class RoomInfoDao extends BaseDao{
	private static final Log logger=LogFactory.getLog(RoomInfoDao.class);
	
	/**
	 * 根据条件分页查询房间信息
	 * @param dgm 分页参数
	 * @param rqp 房间查询参数
	 * @return Page对象（分页信息&分页数据）
	 */
	public Page query(DataGridModel dgm,RoomQueryParam rqp){
		logger.debug("查询房间");
		/* 构建sql */
		StringBuilder sql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		sql.append("select * from roominfo where 1=1");
		if(rqp.getRoomName()!=null&&!"".equals(rqp.getRoomName())){
			sql.append(" and roomName like ?");
			params.add("%"+rqp.getRoomName()+"%");
		}
//		if(uqp.getRoomType()>-1){
//			sql.append(" and roomType=?");
//			params.add(uqp.getRoomType());
//		}
		if(rqp.getSellPrice1()>-1){
			sql.append(" and sellPrice>=?");
			params.add(rqp.getSellPrice1());
		}
		if(rqp.getSellPrice2()>-1){
			sql.append(" and sellPrice<=?");
			params.add(rqp.getSellPrice2());
		}
		
		if(rqp.getBeginDate()!=null){
			sql.append(" and beginDate>=?");
			params.add(DateUtil.ymd(rqp.getBeginDate()));
		}
		if(rqp.getEndDate()!=null){
			sql.append(" and endDate<=?");
			params.add(DateUtil.ymd(rqp.getEndDate()));
		}
		//return super.queryMapByPage(sql.toString(), dgm.getRows(), dgm.getPage(),params);
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params, RoomInfoDomain.class);
	}
	
	public RoomInfoDomain getRoomById(String id){
		String sql="select * from roominfo where roomId=?";
		List<RoomInfoDomain> list= JdbcUtil.queryForList(this, RoomInfoDomain.class, sql, id);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	public RoomInfoDomain getRoomByHotelId(String id){
		String sql="select * from roominfo where hotelId=?";
		List<RoomInfoDomain> list= JdbcUtil.queryForList(this, RoomInfoDomain.class, sql, id);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	/**
	 * 增加一个房间
	 * @param roomInfoDomain
	 */
	public void add(RoomInfoDomain roomInfoDomain){
		super.addObj(roomInfoDomain);
	}
	
	public void update(RoomInfoDomain roomInfoDomain){
		super.updateObj(roomInfoDomain, "roomId");
	}
	
	/**
	 * 批量删除
	 * @param ids
	 */
	public void delete(String[] ids){
		StringBuilder delSql=new StringBuilder();
		delSql.append("delete from roominfo where roomId in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		this.getJdbcTemplate().update(delSql.toString(), ids);
	}
	
}
